﻿using D.B.Data.API.Applications.Command.RoleCommand;
using D.B.Data.Domain;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;

namespace D.B.Data.API.Applications.CommandHandler.RoleCommandHandler
{
    public class DelUserRoleCommandHandler : IRequestHandler<DelUserRoleCommand, APIResult<int>>
    {
        private readonly IBaseRepository<UserRoleModel> baseRepository;

        public DelUserRoleCommandHandler(IBaseRepository<UserRoleModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        Task<APIResult<int>> IRequestHandler<DelUserRoleCommand, APIResult<int>>.Handle(DelUserRoleCommand request, CancellationToken cancellationToken)
        {
            var res = baseRepository.GetAll().FirstOrDefault(s => s.UserId == request.UserId && s.RoleId == request.RoleId)!;

            int count = baseRepository.Del(res);
            APIResult<int> result = new APIResult<int>();

            if (count == 0)
            {
                result.Code=EnumCode.File;
                result.Msg = "删除失败";
                result.Data = count;
            }
            else
            {
                result.Code = EnumCode.Ok;
                result.Msg = "删除成功";
                result.Data = count;
            }

            return Task.FromResult(result);
        }
    }
}
